package com.google.widget.print.core;

import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.UIObject;

/**
 * @author <a href="mailto:todushyant@gmail.com">todushyant@gmail.com</a>
 * 
 */
public class Print {
	public static native void it() /*-{
		$wnd.print();
	}-*/;

	/**
	 * 
	 * <code>
	 * <iframe id="__printingFrame"
	 * style="position: absolute; width: 0; height: 0; border: 0">"</code> must
	 * be there on entrypoint html module to use.
	 * 
	 * @param html
	 */
	public static native void it(String html) /*-{
		var frame = $doc.getElementById('__printingFrame');
		if (!frame) {
			$wnd.alert("Error: Can't find printing frame.");
			return;
		}
		frame = frame.contentWindow;
		var doc = frame.document;
		doc.open();
		doc.write(html);
		doc.close();
		frame.focus();
		frame.print();
	}-*/;

	/**
	 * 
	 * to print {@link UIObject}
	 * 
	 * @param obj
	 */
	public static void it(UIObject obj) {
		it("", obj.getElement().toString());
	}

	/**
	 * to print {@link Element}
	 * 
	 * @param element
	 */
	public static void it(Element element) {
		it("", element.toString());
	}

	/**
	 * to add style to head
	 * 
	 * @param style
	 * @param it
	 */
	public static void it(String style, String it) {
		it("<it><header>" + style + "</header><body>" + it + "</body></it>");
	}

	/**
	 * @param style
	 * @param obj
	 */
	public static void it(String style, UIObject obj) {
		it(style, obj.getElement().toString());
	}

	/**
	 * @param style
	 * @param element
	 */
	public static void it(String style, Element element) {
		it(style, element.toString());
	}
}
